﻿using System;
using System.Collections.Generic;

using System.Text;
using System.IO;
using rkShared.Serialization;

namespace rkShared.BEncode
{
    class BEncodedList : List<IBElement>, IBElement
    {
        #region IBElement Members

        public void Encode(BinaryWriter writer)
        {
            writer.Write('l');

            foreach (IBElement element in this)
                element.Encode(writer);

            writer.Write('e');
        }
        public void Decode(byte[] binary)
        {
            MemoryStream _stream = new MemoryStream(binary);
            BinaryReader _reader = new BinaryReader(_stream);
            _reader.ReadChar(); //Read 'l'

            BEncodedList _dict = BEncodedDocument.DecodeDictioanary(_reader) as BEncodedList;

            this.AddRange(_dict.ToArray());
            _stream.Close();
        }
        public void Add(IBinarySerializable item)
        {
            BEncodedDictionary _dict = new BEncodedDictionary();
            item.SerializeToDictionary(_dict);
            base.Add(_dict);
        }
        #endregion
    }
}
